package com.zhao.base;
  
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhao.model.AccessToken;
import com.zhao.model.WeixinOauth2Token;
import com.zhao.util.CommonUtil;
import com.zhao.util.HttpRequest;
import com.zhao.util.StringUtil;

import net.sf.json.JSONObject;
/**
 * 获取accessToken
 * @author zhaowj
 * @date2018年1月21日 下午1:18:58
 */
public class AccessTokenBase {
	
	private static Logger log = LoggerFactory.getLogger(AccessTokenBase.class);  
	private static String token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET";
	
	public static AccessToken getAccessToken(String appid,String appsecret) {

		token_url=token_url.replace("APPID",appid);
		token_url=token_url.replace("SECRET",appsecret);
		
		JSONObject json=HttpRequest.httpsRequest(token_url, "GET",null); 	
		String accessToken=json.getString("access_token");
		if(!StringUtil.isEmpty(accessToken)) {
			String expiresIn=json.getString("expires_in");
			AccessToken token=new AccessToken(accessToken,Integer.parseInt(expiresIn));
			return token;
		}else {
			log.info(json.toString()); 
		}
		return null;
	}
	
	public static String getAccessTokenStr() {
		String[] signArr=new String[] {"appId","appsecret","token"};
		Map<String,String> signMap=CommonUtil.readValue("/wx.properties",signArr);
		token_url=token_url.replace("APPID",signMap.get("appId"));
		token_url=token_url.replace("SECRET",signMap.get("appsecret"));
		JSONObject json=HttpRequest.httpsRequest(token_url, "GET",null); 	
		String accessToken=json.getString("access_token");
		if(!StringUtil.isEmpty(accessToken)) {
			String expiresIn=json.getString("expires_in");
			AccessToken token=new AccessToken(accessToken,Integer.parseInt(expiresIn));
			return token.getAccessToken();
		}else {
			log.info(json.toString()); 
		}
		return null;
	}
	
	/**
	 * 获取网页授权access_token
	 * @param appId
	 * @param appSecret
	 * @param code
	 * @return
	 */
	  public static WeixinOauth2Token getOauth2AccessToken(String appId,String appSecret,String code)
	    {
	    	
	    	WeixinOauth2Token wat=null;
	    	
	    	String requestUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
	
	    	requestUrl=requestUrl.replace("APPID", appId);
	
	    	requestUrl=requestUrl.replace("SECRET", appSecret);
	
	    	requestUrl=requestUrl.replace("CODE", code);
	    	
	    
	    	JSONObject jsonObject=HttpRequest.httpsRequest(requestUrl, "GET", null);
	    
	    	if (jsonObject!=null) {
	    		
				try{
				
					wat=new WeixinOauth2Token();
				
					wat.setAccessToken(jsonObject.getString("access_token"));
			
					wat.setExpiresIn(jsonObject.getInt("expires_in"));
		
					wat.setRefreshToken(jsonObject.getString("refresh_token"));
					
					wat.setOpenId(jsonObject.getString("openid"));
			
					wat.setScope(jsonObject.getString("scope"));
					
				}
				catch(Exception e){
					wat=null;
					int errorCode=jsonObject.getInt("errcode");
					String errorMsg=jsonObject.getString("errmsg");
					log.error("获取用户信息异常:{}  errmsg:{}",errorCode,errorMsg);
					
				}
			}
	    	return wat;
	    }
	
}
